using Vin.Win32.Input.Enum;
using Win32;

namespace Vin.Win32.Input;

public class VinKeyboardSimulator
{
    public static VinKeyboardSimulator Current { get; } = new VinKeyboardSimulator();

    public VinKeyboardSimulator SendInput(params INPUT[] inputs)
    {
        User32.SendInput((uint)inputs.Length, inputs, INPUT.Size);
        return this;
    }

    public VinKeyboardSimulator KeyDown(VinVirtualKeyCode keyCode)
    {
        var inputArr = new VinInputBuilder().AddKeyDown(keyCode).ToArray();
        return SendInput(inputArr);
    }

    public VinKeyboardSimulator KeyUp(VinVirtualKeyCode keyCode)
    {
        var inputArr = new VinInputBuilder().AddKeyUp(keyCode).ToArray();
        return SendInput(inputArr);
    }

    public VinKeyboardSimulator KeyPress(VinVirtualKeyCode keyCode)
    {
        return KeyPress(new[] { keyCode });
    }

    public VinKeyboardSimulator KeyPress(params VinVirtualKeyCode[] keyCodes)
    {
        var inputBuilder = new VinInputBuilder();
        foreach (var keyCode in keyCodes)
        {
            inputBuilder.AddKeyDown(keyCode);
        }

        foreach (var keyCode in keyCodes.Reverse())
        {
            inputBuilder.AddKeyUp(keyCode);
        }

        return SendInput(inputBuilder.ToArray());
    }
    
    public VinKeyboardSimulator SendText(string msg)
    {
        var inputArr = new VinInputBuilder().TextEntry(msg).ToArray();
        return SendInput(inputArr);
    }
}